{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-28T09:47:13.686194Z",
     "start_time": "2019-05-28T09:47:13.290839Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "import numpy as np\n",
    "\n",
    "from sklearn.preprocessing import LabelEncoder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-28T09:47:13.885441Z",
     "start_time": "2019-05-28T09:47:13.848853Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "df_train = pd.read_csv('../input/train_face_value_label.csv', dtype={' label': object, 'name': object})\n",
    "lbl = LabelEncoder()\n",
    "df_train['y'] = lbl.fit_transform(df_train[' label'].values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-28T09:47:14.415573Z",
     "start_time": "2019-05-28T09:47:14.141033Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "import os, sys, glob, argparse\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from tqdm import tqdm\n",
    "\n",
    "import time, datetime\n",
    "import pdb, traceback\n",
    "\n",
    "import cv2\n",
    "# import imagehash\n",
    "from PIL import Image\n",
    "\n",
    "from sklearn.model_selection import train_test_split, StratifiedKFold\n",
    "\n",
    "import torch\n",
    "torch.manual_seed(0)\n",
    "torch.backends.cudnn.deterministic = False\n",
    "torch.backends.cudnn.benchmark = True\n",
    "\n",
    "import torchvision.models as models\n",
    "import torchvision.transforms as transforms\n",
    "import torchvision.datasets as datasets\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "import torch.optim as optim\n",
    "from torch.autograd import Variable\n",
    "from torch.utils.data.dataset import Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-28T09:47:14.678727Z",
     "start_time": "2019-05-28T09:47:14.665979Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "class QRDataset(Dataset):\n",
    "    def __init__(self, img_path, img_label, transform=None):\n",
    "        self.img_path = img_path\n",
    "        self.img_label=img_label\n",
    "        \n",
    "        if transform is not None:\n",
    "            self.transform = transform\n",
    "        else:\n",
    "            self.transform = None\n",
    "    \n",
    "    def __getitem__(self, index):\n",
    "        start_time = time.time()\n",
    "        img = Image.open(self.img_path[index])\n",
    "        \n",
    "        if self.transform is not None:\n",
    "            img = self.transform(img)\n",
    "                \n",
    "        return img, torch.from_numpy(np.array([self.img_label[index]]))\n",
    "    \n",
    "    def __len__(self):\n",
    "        return len(self.img_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-28T09:47:14.993840Z",
     "start_time": "2019-05-28T09:47:14.909837Z"
    }
   },
   "outputs": [],
   "source": [
    "train_path = ['../input/train_data/'+x for x in df_train['name']]\n",
    "train_label = df_train['y'].values\n",
    "\n",
    "test_path = glob.glob('../input/public_test_data/*.jpg')\n",
    "\n",
    "train_path, train_label = np.array(train_path), np.array(train_label)\n",
    "test_path = np.array(test_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-28T09:47:15.368672Z",
     "start_time": "2019-05-28T09:47:15.348682Z"
    }
   },
   "outputs": [],
   "source": [
    "def accuracy(output, target, topk=(1,)):\n",
    "    \"\"\"Computes the accuracy over the k top predictions for the specified values of k\"\"\"\n",
    "    with torch.no_grad():\n",
    "        maxk = max(topk)\n",
    "        batch_size = target.size(0)\n",
    "\n",
    "        _, pred = output.topk(maxk, 1, True, True)\n",
    "        pred = pred.t()\n",
    "        correct = pred.eq(target.view(1, -1).expand_as(pred))\n",
    "\n",
    "        res = []\n",
    "        for k in topk:\n",
    "            correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)\n",
    "            res.append(correct_k.mul_(100.0 / batch_size))\n",
    "        return res\n",
    "    \n",
    "class AverageMeter(object):\n",
    "    \"\"\"Computes and stores the average and current value\"\"\"\n",
    "    def __init__(self, name, fmt=':f'):\n",
    "        self.name = name\n",
    "        self.fmt = fmt\n",
    "        self.reset()\n",
    "\n",
    "    def reset(self):\n",
    "        self.val = 0\n",
    "        self.avg = 0\n",
    "        self.sum = 0\n",
    "        self.count = 0\n",
    "\n",
    "    def update(self, val, n=1):\n",
    "        self.val = val\n",
    "        self.sum += val * n\n",
    "        self.count += n\n",
    "        self.avg = self.sum / self.count\n",
    "\n",
    "    def __str__(self):\n",
    "        fmtstr = '{name} {val' + self.fmt + '} ({avg' + self.fmt + '})'\n",
    "        return fmtstr.format(**self.__dict__)\n",
    "\n",
    "class ProgressMeter(object):\n",
    "    def __init__(self, num_batches, *meters):\n",
    "        self.batch_fmtstr = self._get_batch_fmtstr(num_batches)\n",
    "        self.meters = meters\n",
    "        self.prefix = \"\"\n",
    "\n",
    "\n",
    "    def pr2int(self, batch):\n",
    "        entries = [self.prefix + self.batch_fmtstr.format(batch)]\n",
    "        entries += [str(meter) for meter in self.meters]\n",
    "        print('\\t'.join(entries))\n",
    "\n",
    "    def _get_batch_fmtstr(self, num_batches):\n",
    "        num_digits = len(str(num_batches // 1))\n",
    "        fmt = '{:' + str(num_digits) + 'd}'\n",
    "        return '[' + fmt + '/' + fmt.format(num_batches) + ']'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-28T09:47:17.281360Z",
     "start_time": "2019-05-28T09:47:17.254973Z"
    }
   },
   "outputs": [],
   "source": [
    "class VisitNet(nn.Module):\n",
    "    def __init__(self):\n",
    "        super(VisitNet, self).__init__()\n",
    "        model = models.resnet18(True)\n",
    "        model.avgpool = nn.AdaptiveAvgPool2d(1)\n",
    "        model.fc = nn.Linear(512, 256)\n",
    "        self.resnet = model\n",
    "        \n",
    "    def forward(self, img):\n",
    "        out = self.resnet(img)\n",
    "        return F.log_softmax(out, dim=1)\n",
    "\n",
    "def validate(val_loader, model, criterion):\n",
    "    batch_time = AverageMeter('Time', ':6.3f')\n",
    "    losses = AverageMeter('Loss', ':.4e')\n",
    "    top1 = AverageMeter('Acc@1', ':6.2f')\n",
    "    top5 = AverageMeter('Acc@5', ':6.2f')\n",
    "    progress = ProgressMeter(len(val_loader), batch_time, losses, top1, top5)\n",
    "\n",
    "    # switch to evaluate mode\n",
    "    model.eval()\n",
    "\n",
    "    with torch.no_grad():\n",
    "        end = time.time()\n",
    "        for i, (input, target) in enumerate(val_loader):\n",
    "            input = input.cuda()\n",
    "            target = target.cuda()\n",
    "\n",
    "            # compute output\n",
    "            output = model(input)\n",
    "            loss = criterion(output, torch.max(target, 1)[0])\n",
    "\n",
    "            # measure accuracy and record loss\n",
    "            acc1, acc5 = accuracy(output, target, topk=(1, 5))\n",
    "            losses.update(loss.item(), input.size(0))\n",
    "            top1.update(acc1[0], input.size(0))\n",
    "            top5.update(acc5[0], input.size(0))\n",
    "\n",
    "            # measure elapsed time\n",
    "            batch_time.update(time.time() - end)\n",
    "            end = time.time()\n",
    "\n",
    "        # TODO: this should also be done with the ProgressMeter\n",
    "        print(' * Acc@1 {top1.avg:.3f} Acc@5 {top5.avg:.3f}'\n",
    "              .format(top1=top1, top5=top5))\n",
    "        return top1\n",
    "\n",
    "def predict(test_loader, model, tta=10):\n",
    "    # switch to evaluate mode\n",
    "    model.eval()\n",
    "    \n",
    "    test_pred_tta = None\n",
    "    for _ in range(tta):\n",
    "        test_pred = []\n",
    "        with torch.no_grad():\n",
    "            end = time.time()\n",
    "            for i, (input, target) in enumerate(test_loader):\n",
    "                input = input.cuda()\n",
    "                target = target.cuda()\n",
    "\n",
    "                # compute output\n",
    "                output = model(input)\n",
    "                output = output.data.cpu().numpy()\n",
    "\n",
    "                test_pred.append(output)\n",
    "        test_pred = np.vstack(test_pred)\n",
    "    \n",
    "        if test_pred_tta is None:\n",
    "            test_pred_tta = test_pred\n",
    "        else:\n",
    "            test_pred_tta += test_pred\n",
    "    \n",
    "    return test_pred_tta\n",
    "\n",
    "def train(train_loader, model, criterion, optimizer, epoch):\n",
    "    batch_time = AverageMeter('Time', ':6.3f')\n",
    "    # data_time = AverageMeter('Data', ':6.3f')\n",
    "    losses = AverageMeter('Loss', ':.4e')\n",
    "    top1 = AverageMeter('Acc@1', ':6.2f')\n",
    "    # top5 = AverageMeter('Acc@5', ':6.2f')\n",
    "    progress = ProgressMeter(len(train_loader), batch_time, losses, top1)\n",
    "\n",
    "    # switch to train mode\n",
    "    model.train()\n",
    "\n",
    "    end = time.time()\n",
    "    for i, (input, target) in enumerate(train_loader):\n",
    "        input = input.cuda(non_blocking=True)\n",
    "        target = target.cuda(non_blocking=True)\n",
    "\n",
    "        # compute output\n",
    "        output = model(input)\n",
    "        loss = criterion(output, torch.max(target, 1)[0])\n",
    "\n",
    "        # measure accuracy and record loss\n",
    "        acc1, acc5 = accuracy(output, target, topk=(1, 5))\n",
    "        losses.update(loss.item(), input.size(0))\n",
    "        top1.update(acc1[0], input.size(0))\n",
    "        # top5.update(acc5[0], input.size(0))\n",
    "\n",
    "        # compute gradient and do SGD step\n",
    "        optimizer.zero_grad()\n",
    "        loss.backward()\n",
    "        optimizer.step()\n",
    "\n",
    "        # measure elapsed time\n",
    "        batch_time.update(time.time() - end)\n",
    "        end = time.time()\n",
    "\n",
    "        if i % 100 == 0:\n",
    "            progress.pr2int(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-28T10:43:46.408371Z",
     "start_time": "2019-05-28T09:47:17.877474Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 [ 3693  3694  3698 ... 39617 39618 39619] [   0    1    2 ... 4238 4244 4259]\n",
      "Epoch:  0\n",
      "[  0/714]\tTime  3.859 ( 3.859)\tLoss 5.8133e+00 (5.8133e+00)\tAcc@1   0.00 (  0.00)\n",
      "[100/714]\tTime  1.849 ( 0.288)\tLoss 2.9272e+00 (2.0695e+00)\tAcc@1  24.00 ( 25.72)\n",
      "[200/714]\tTime  0.094 ( 0.262)\tLoss 1.6259e+00 (1.8834e+00)\tAcc@1  16.00 ( 29.29)\n",
      "[300/714]\tTime  0.093 ( 0.259)\tLoss 1.5794e+00 (1.7069e+00)\tAcc@1  46.00 ( 36.07)\n",
      "[400/714]\tTime  0.095 ( 0.255)\tLoss 8.3924e-01 (1.5711e+00)\tAcc@1  72.00 ( 41.93)\n",
      "[500/714]\tTime  0.355 ( 0.252)\tLoss 5.1510e-01 (1.4221e+00)\tAcc@1  80.00 ( 47.67)\n",
      "[600/714]\tTime  0.095 ( 0.252)\tLoss 7.2558e-01 (1.2902e+00)\tAcc@1  78.00 ( 52.96)\n",
      "[700/714]\tTime  0.096 ( 0.251)\tLoss 4.2546e-01 (1.1758e+00)\tAcc@1  80.00 ( 57.26)\n",
      " * Acc@1 85.909 Acc@5 99.899\n",
      "Epoch:  1\n",
      "[  0/714]\tTime  2.943 ( 2.943)\tLoss 2.8106e-01 (2.8106e-01)\tAcc@1  86.00 ( 86.00)\n",
      "[100/714]\tTime  0.095 ( 0.268)\tLoss 5.8633e-01 (3.6356e-01)\tAcc@1  76.00 ( 87.45)\n",
      "[200/714]\tTime  0.093 ( 0.255)\tLoss 1.4462e-01 (3.4471e-01)\tAcc@1  96.00 ( 88.29)\n",
      "[300/714]\tTime  0.091 ( 0.257)\tLoss 2.1092e-01 (3.1815e-01)\tAcc@1  96.00 ( 89.34)\n",
      "[400/714]\tTime  0.095 ( 0.255)\tLoss 3.0392e-01 (2.9124e-01)\tAcc@1  88.00 ( 90.07)\n",
      "[500/714]\tTime  0.095 ( 0.253)\tLoss 1.6030e-01 (2.6318e-01)\tAcc@1  96.00 ( 91.05)\n",
      "[600/714]\tTime  0.095 ( 0.253)\tLoss 2.0345e-01 (2.4410e-01)\tAcc@1  94.00 ( 91.70)\n",
      "[700/714]\tTime  0.506 ( 0.252)\tLoss 6.6352e-02 (2.2697e-01)\tAcc@1 100.00 ( 92.36)\n",
      " * Acc@1 98.891 Acc@5 100.000\n",
      "Epoch:  2\n",
      "[  0/714]\tTime  2.183 ( 2.183)\tLoss 1.5926e-01 (1.5926e-01)\tAcc@1  96.00 ( 96.00)\n",
      "[100/714]\tTime  1.463 ( 0.280)\tLoss 1.3053e-02 (1.0568e-01)\tAcc@1 100.00 ( 96.95)\n",
      "[200/714]\tTime  0.093 ( 0.263)\tLoss 2.3566e-02 (7.9397e-02)\tAcc@1  98.00 ( 97.53)\n",
      "[300/714]\tTime  0.103 ( 0.260)\tLoss 5.7558e-03 (6.7921e-02)\tAcc@1 100.00 ( 97.94)\n",
      "[400/714]\tTime  0.095 ( 0.257)\tLoss 8.4421e-03 (6.9756e-02)\tAcc@1 100.00 ( 97.96)\n",
      "[500/714]\tTime  0.095 ( 0.256)\tLoss 4.1006e-02 (6.4362e-02)\tAcc@1  98.00 ( 98.12)\n",
      "[600/714]\tTime  0.095 ( 0.255)\tLoss 4.7512e-04 (5.8159e-02)\tAcc@1 100.00 ( 98.30)\n",
      "[700/714]\tTime  0.092 ( 0.253)\tLoss 1.1244e-02 (5.7544e-02)\tAcc@1 100.00 ( 98.38)\n",
      " * Acc@1 94.958 Acc@5 99.924\n",
      "Epoch:  3\n",
      "[  0/714]\tTime  2.956 ( 2.956)\tLoss 3.5582e-03 (3.5582e-03)\tAcc@1 100.00 (100.00)\n",
      "[100/714]\tTime  1.431 ( 0.285)\tLoss 3.6302e-02 (2.5984e-02)\tAcc@1  98.00 ( 99.29)\n",
      "[200/714]\tTime  0.095 ( 0.258)\tLoss 1.3744e-01 (4.6076e-02)\tAcc@1  94.00 ( 98.76)\n",
      "[300/714]\tTime  0.094 ( 0.256)\tLoss 6.6145e-04 (3.8794e-02)\tAcc@1 100.00 ( 99.02)\n",
      "[400/714]\tTime  0.096 ( 0.255)\tLoss 1.0336e-03 (3.4691e-02)\tAcc@1 100.00 ( 99.17)\n",
      "[500/714]\tTime  0.087 ( 0.252)\tLoss 1.5401e-02 (3.0321e-02)\tAcc@1 100.00 ( 99.27)\n",
      "[600/714]\tTime  1.053 ( 0.252)\tLoss 3.2757e-03 (2.8641e-02)\tAcc@1 100.00 ( 99.32)\n",
      "[700/714]\tTime  0.091 ( 0.252)\tLoss 1.0956e-01 (2.8250e-02)\tAcc@1  98.00 ( 99.32)\n",
      " * Acc@1 98.588 Acc@5 100.000\n",
      "Epoch:  4\n",
      "[  0/714]\tTime  2.191 ( 2.191)\tLoss 5.0165e-04 (5.0165e-04)\tAcc@1 100.00 (100.00)\n",
      "[100/714]\tTime  1.557 ( 0.287)\tLoss 6.2601e-04 (3.6502e-02)\tAcc@1 100.00 ( 99.23)\n",
      "[200/714]\tTime  0.097 ( 0.262)\tLoss 1.3960e-02 (2.8681e-02)\tAcc@1 100.00 ( 99.41)\n",
      "[300/714]\tTime  0.092 ( 0.257)\tLoss 2.8335e-03 (2.5414e-02)\tAcc@1 100.00 ( 99.53)\n",
      "[400/714]\tTime  0.093 ( 0.253)\tLoss 6.6917e-04 (2.2766e-02)\tAcc@1 100.00 ( 99.61)\n",
      "[500/714]\tTime  0.095 ( 0.255)\tLoss 1.7261e-03 (2.1138e-02)\tAcc@1 100.00 ( 99.65)\n",
      "[600/714]\tTime  0.094 ( 0.253)\tLoss 4.1532e-01 (2.0283e-02)\tAcc@1  98.00 ( 99.68)\n",
      "[700/714]\tTime  0.097 ( 0.253)\tLoss 2.2805e-03 (1.9063e-02)\tAcc@1 100.00 ( 99.71)\n",
      " * Acc@1 99.899 Acc@5 100.000\n",
      "Epoch:  5\n",
      "[  0/714]\tTime  3.146 ( 3.146)\tLoss 3.3225e-03 (3.3225e-03)\tAcc@1 100.00 (100.00)\n",
      "[100/714]\tTime  0.094 ( 0.271)\tLoss 7.9985e-04 (3.7710e-02)\tAcc@1 100.00 ( 98.93)\n",
      "[200/714]\tTime  0.094 ( 0.263)\tLoss 2.5798e-04 (2.9430e-02)\tAcc@1 100.00 ( 99.20)\n",
      "[300/714]\tTime  0.094 ( 0.257)\tLoss 1.1583e-03 (2.2827e-02)\tAcc@1 100.00 ( 99.42)\n",
      "[400/714]\tTime  0.095 ( 0.255)\tLoss 1.5971e-03 (2.0093e-02)\tAcc@1 100.00 ( 99.53)\n",
      "[500/714]\tTime  0.087 ( 0.254)\tLoss 6.6752e-04 (2.0082e-02)\tAcc@1 100.00 ( 99.55)\n",
      "[600/714]\tTime  0.095 ( 0.253)\tLoss 8.0071e-04 (1.8431e-02)\tAcc@1 100.00 ( 99.61)\n",
      "[700/714]\tTime  0.093 ( 0.253)\tLoss 1.0834e-02 (2.4141e-02)\tAcc@1 100.00 ( 99.52)\n",
      " * Acc@1 85.329 Acc@5 99.874\n",
      "Epoch:  6\n",
      "[  0/714]\tTime  2.741 ( 2.741)\tLoss 4.9033e-03 (4.9033e-03)\tAcc@1 100.00 (100.00)\n",
      "[100/714]\tTime  0.094 ( 0.268)\tLoss 3.7268e-04 (6.5468e-02)\tAcc@1 100.00 ( 98.36)\n",
      "[200/714]\tTime  0.478 ( 0.262)\tLoss 1.6002e-03 (4.2079e-02)\tAcc@1 100.00 ( 99.01)\n",
      "[300/714]\tTime  0.094 ( 0.260)\tLoss 3.4535e-03 (3.7530e-02)\tAcc@1 100.00 ( 99.22)\n",
      "[400/714]\tTime  0.095 ( 0.259)\tLoss 4.0416e-03 (2.9911e-02)\tAcc@1 100.00 ( 99.38)\n",
      "[500/714]\tTime  0.094 ( 0.257)\tLoss 1.8098e-03 (2.6927e-02)\tAcc@1 100.00 ( 99.45)\n",
      "[600/714]\tTime  0.093 ( 0.257)\tLoss 1.2791e-03 (2.4828e-02)\tAcc@1 100.00 ( 99.51)\n",
      "[700/714]\tTime  0.094 ( 0.254)\tLoss 7.8258e-03 (2.2058e-02)\tAcc@1 100.00 ( 99.57)\n",
      " * Acc@1 99.950 Acc@5 100.000\n",
      "Epoch:  7\n",
      "[  0/714]\tTime  2.884 ( 2.884)\tLoss 6.1946e-04 (6.1946e-04)\tAcc@1 100.00 (100.00)\n",
      "[100/714]\tTime  0.982 ( 0.274)\tLoss 7.8061e-02 (8.9849e-02)\tAcc@1  94.00 ( 97.50)\n",
      "[200/714]\tTime  0.094 ( 0.262)\tLoss 6.8690e-03 (5.2803e-02)\tAcc@1 100.00 ( 98.66)\n",
      "[300/714]\tTime  0.095 ( 0.259)\tLoss 4.1534e-03 (4.1668e-02)\tAcc@1 100.00 ( 99.04)\n",
      "[400/714]\tTime  0.093 ( 0.254)\tLoss 6.7321e-04 (3.1824e-02)\tAcc@1 100.00 ( 99.27)\n",
      "[500/714]\tTime  0.093 ( 0.254)\tLoss 5.2389e-04 (3.0485e-02)\tAcc@1 100.00 ( 99.37)\n",
      "[600/714]\tTime  0.088 ( 0.253)\tLoss 6.5615e-04 (2.6762e-02)\tAcc@1 100.00 ( 99.46)\n",
      "[700/714]\tTime  0.094 ( 0.251)\tLoss 2.1521e-04 (2.3866e-02)\tAcc@1 100.00 ( 99.52)\n",
      " * Acc@1 98.488 Acc@5 100.000\n",
      "Epoch:  8\n",
      "[  0/714]\tTime  2.994 ( 2.994)\tLoss 2.1949e-03 (2.1949e-03)\tAcc@1 100.00 (100.00)\n",
      "[100/714]\tTime  0.094 ( 0.268)\tLoss 6.7557e-03 (5.4904e-02)\tAcc@1 100.00 ( 98.38)\n",
      "[200/714]\tTime  0.094 ( 0.258)\tLoss 5.5535e-04 (3.3668e-02)\tAcc@1 100.00 ( 99.06)\n",
      "[300/714]\tTime  0.692 ( 0.255)\tLoss 2.9001e-04 (2.9153e-02)\tAcc@1 100.00 ( 99.32)\n",
      "[400/714]\tTime  1.397 ( 0.256)\tLoss 3.9163e-03 (2.6622e-02)\tAcc@1 100.00 ( 99.46)\n",
      "[500/714]\tTime  0.093 ( 0.254)\tLoss 3.7470e-03 (2.3485e-02)\tAcc@1 100.00 ( 99.54)\n",
      "[600/714]\tTime  0.838 ( 0.253)\tLoss 6.4116e-04 (2.0897e-02)\tAcc@1 100.00 ( 99.60)\n",
      "[700/714]\tTime  0.094 ( 0.253)\tLoss 5.1112e-04 (1.9254e-02)\tAcc@1 100.00 ( 99.64)\n",
      " * Acc@1 100.000 Acc@5 100.000\n",
      "Epoch:  9\n",
      "[  0/714]\tTime  3.225 ( 3.225)\tLoss 5.4796e-04 (5.4796e-04)\tAcc@1 100.00 (100.00)\n",
      "[100/714]\tTime  0.095 ( 0.273)\tLoss 6.0222e-04 (5.3034e-03)\tAcc@1 100.00 ( 99.94)\n",
      "[200/714]\tTime  0.093 ( 0.260)\tLoss 2.5573e-03 (4.4673e-03)\tAcc@1 100.00 ( 99.93)\n",
      "[300/714]\tTime  0.093 ( 0.255)\tLoss 3.4179e-04 (4.1883e-03)\tAcc@1 100.00 ( 99.94)\n",
      "[400/714]\tTime  0.496 ( 0.256)\tLoss 1.4969e-03 (7.1877e-03)\tAcc@1 100.00 ( 99.92)\n",
      "[500/714]\tTime  0.092 ( 0.252)\tLoss 1.6138e-03 (1.0214e-02)\tAcc@1 100.00 ( 99.89)\n",
      "[600/714]\tTime  0.093 ( 0.253)\tLoss 1.9289e-04 (9.4727e-03)\tAcc@1 100.00 ( 99.90)\n",
      "[700/714]\tTime  0.092 ( 0.253)\tLoss 7.7244e-04 (8.9381e-03)\tAcc@1 100.00 ( 99.91)\n",
      " * Acc@1 100.000 Acc@5 100.000\n",
      "Epoch:  10\n",
      "[  0/714]\tTime  3.271 ( 3.271)\tLoss 3.0337e-04 (3.0337e-04)\tAcc@1 100.00 (100.00)\n",
      "[100/714]\tTime  0.095 ( 0.272)\tLoss 5.1049e-04 (4.4710e-03)\tAcc@1 100.00 ( 99.92)\n",
      "[200/714]\tTime  0.094 ( 0.264)\tLoss 1.9491e-02 (9.3617e-03)\tAcc@1  98.00 ( 99.89)\n",
      "[300/714]\tTime  0.093 ( 0.258)\tLoss 6.6108e-04 (1.0438e-02)\tAcc@1 100.00 ( 99.86)\n",
      "[400/714]\tTime  0.094 ( 0.255)\tLoss 1.3686e-02 (1.0391e-02)\tAcc@1 100.00 ( 99.86)\n",
      "[500/714]\tTime  0.095 ( 0.255)\tLoss 2.3074e-04 (1.0790e-02)\tAcc@1 100.00 ( 99.85)\n",
      "[600/714]\tTime  0.092 ( 0.252)\tLoss 4.8460e-04 (1.0076e-02)\tAcc@1 100.00 ( 99.86)\n",
      "[700/714]\tTime  0.089 ( 0.252)\tLoss 4.9155e-04 (1.0229e-02)\tAcc@1 100.00 ( 99.87)\n",
      " * Acc@1 99.899 Acc@5 100.000\n",
      "Epoch:  11\n",
      "[  0/714]\tTime  2.181 ( 2.181)\tLoss 1.1295e-03 (1.1295e-03)\tAcc@1 100.00 (100.00)\n",
      "[100/714]\tTime  0.096 ( 0.267)\tLoss 1.9320e-04 (1.8409e-02)\tAcc@1 100.00 ( 99.64)\n",
      "[200/714]\tTime  0.095 ( 0.262)\tLoss 1.2557e-03 (1.4680e-02)\tAcc@1 100.00 ( 99.73)\n",
      "[300/714]\tTime  0.104 ( 0.255)\tLoss 4.3841e-03 (1.3290e-02)\tAcc@1 100.00 ( 99.79)\n",
      "[400/714]\tTime  0.094 ( 0.251)\tLoss 3.5665e-03 (1.2619e-02)\tAcc@1 100.00 ( 99.81)\n",
      "[500/714]\tTime  0.090 ( 0.252)\tLoss 1.8143e-03 (1.2996e-02)\tAcc@1 100.00 ( 99.83)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[600/714]\tTime  0.897 ( 0.250)\tLoss 1.5600e-04 (1.1546e-02)\tAcc@1 100.00 ( 99.85)\n",
      "[700/714]\tTime  0.937 ( 0.252)\tLoss 1.4976e-04 (1.0497e-02)\tAcc@1 100.00 ( 99.87)\n",
      " * Acc@1 100.000 Acc@5 100.000\n",
      "Epoch:  12\n",
      "[  0/714]\tTime  2.608 ( 2.608)\tLoss 7.5469e-05 (7.5469e-05)\tAcc@1 100.00 (100.00)\n",
      "[100/714]\tTime  0.931 ( 0.277)\tLoss 8.0623e-05 (2.6638e-03)\tAcc@1 100.00 ( 99.98)\n",
      "[200/714]\tTime  1.737 ( 0.268)\tLoss 6.2690e-04 (6.0161e-03)\tAcc@1 100.00 ( 99.92)\n",
      "[300/714]\tTime  1.080 ( 0.260)\tLoss 2.3928e-03 (8.4330e-03)\tAcc@1 100.00 ( 99.91)\n",
      "[400/714]\tTime  0.094 ( 0.252)\tLoss 4.6100e-04 (9.8888e-03)\tAcc@1 100.00 ( 99.89)\n",
      "[500/714]\tTime  0.090 ( 0.253)\tLoss 1.7176e-03 (1.0075e-02)\tAcc@1 100.00 ( 99.89)\n",
      "[600/714]\tTime  0.095 ( 0.252)\tLoss 2.7989e-03 (9.4787e-03)\tAcc@1 100.00 ( 99.90)\n",
      "[700/714]\tTime  0.094 ( 0.252)\tLoss 6.3046e-04 (9.4623e-03)\tAcc@1 100.00 ( 99.89)\n",
      " * Acc@1 100.000 Acc@5 100.000\n",
      "Epoch:  13\n",
      "[  0/714]\tTime  3.042 ( 3.042)\tLoss 1.1733e-04 (1.1733e-04)\tAcc@1 100.00 (100.00)\n",
      "[100/714]\tTime  1.636 ( 0.289)\tLoss 1.5240e-03 (1.7084e-02)\tAcc@1 100.00 ( 99.86)\n",
      "[200/714]\tTime  0.096 ( 0.258)\tLoss 6.3698e-04 (1.5888e-02)\tAcc@1 100.00 ( 99.87)\n",
      "[300/714]\tTime  0.094 ( 0.253)\tLoss 6.0338e-04 (1.2582e-02)\tAcc@1 100.00 ( 99.89)\n",
      "[400/714]\tTime  0.094 ( 0.255)\tLoss 1.1051e-03 (1.0672e-02)\tAcc@1 100.00 ( 99.90)\n",
      "[500/714]\tTime  0.095 ( 0.256)\tLoss 1.0676e-04 (9.2068e-03)\tAcc@1 100.00 ( 99.92)\n",
      "[600/714]\tTime  0.093 ( 0.254)\tLoss 2.2347e-03 (9.2417e-03)\tAcc@1 100.00 ( 99.92)\n",
      "[700/714]\tTime  0.094 ( 0.253)\tLoss 1.7746e-03 (8.9770e-03)\tAcc@1 100.00 ( 99.91)\n",
      " * Acc@1 100.000 Acc@5 100.000\n",
      "Epoch:  14\n",
      "[  0/714]\tTime  3.211 ( 3.211)\tLoss 2.7730e-04 (2.7730e-04)\tAcc@1 100.00 (100.00)\n",
      "[100/714]\tTime  1.020 ( 0.283)\tLoss 1.1611e-04 (1.0118e-02)\tAcc@1 100.00 ( 99.90)\n",
      "[200/714]\tTime  0.094 ( 0.262)\tLoss 7.5515e-04 (8.5705e-03)\tAcc@1 100.00 ( 99.91)\n",
      "[300/714]\tTime  0.094 ( 0.256)\tLoss 1.4104e-03 (8.5742e-03)\tAcc@1 100.00 ( 99.92)\n",
      "[400/714]\tTime  0.092 ( 0.254)\tLoss 1.8015e-03 (8.8037e-03)\tAcc@1 100.00 ( 99.93)\n",
      "[500/714]\tTime  0.094 ( 0.252)\tLoss 3.5376e-04 (9.3981e-03)\tAcc@1 100.00 ( 99.91)\n",
      "[600/714]\tTime  0.825 ( 0.253)\tLoss 5.4216e-04 (8.8880e-03)\tAcc@1 100.00 ( 99.90)\n",
      "[700/714]\tTime  0.094 ( 0.253)\tLoss 7.1063e-04 (8.5223e-03)\tAcc@1 100.00 ( 99.91)\n",
      " * Acc@1 99.950 Acc@5 100.000\n",
      "Epoch:  15\n",
      "[  0/714]\tTime  3.042 ( 3.042)\tLoss 3.7465e-03 (3.7465e-03)\tAcc@1 100.00 (100.00)\n",
      "[100/714]\tTime  0.782 ( 0.271)\tLoss 1.0793e-04 (2.7980e-03)\tAcc@1 100.00 ( 99.98)\n",
      "[200/714]\tTime  0.094 ( 0.254)\tLoss 9.7565e-04 (5.6156e-03)\tAcc@1 100.00 ( 99.95)\n",
      "[300/714]\tTime  0.093 ( 0.255)\tLoss 1.4058e-03 (9.9217e-03)\tAcc@1 100.00 ( 99.93)\n",
      "[400/714]\tTime  0.094 ( 0.254)\tLoss 2.9509e-04 (7.7662e-03)\tAcc@1 100.00 ( 99.94)\n",
      "[500/714]\tTime  0.090 ( 0.253)\tLoss 4.9513e-04 (8.5985e-03)\tAcc@1 100.00 ( 99.93)\n",
      "[600/714]\tTime  0.157 ( 0.254)\tLoss 6.0049e-03 (8.5462e-03)\tAcc@1 100.00 ( 99.93)\n",
      "[700/714]\tTime  0.094 ( 0.252)\tLoss 4.6003e-02 (8.4118e-03)\tAcc@1  98.00 ( 99.93)\n",
      " * Acc@1 99.546 Acc@5 100.000\n",
      "Epoch:  16\n",
      "[  0/714]\tTime  3.067 ( 3.067)\tLoss 1.9468e-02 (1.9468e-02)\tAcc@1 100.00 (100.00)\n",
      "[100/714]\tTime  0.088 ( 0.272)\tLoss 8.2238e-04 (8.0587e-03)\tAcc@1 100.00 ( 99.92)\n",
      "[200/714]\tTime  0.095 ( 0.262)\tLoss 6.8048e-03 (7.3314e-03)\tAcc@1 100.00 ( 99.93)\n",
      "[300/714]\tTime  0.094 ( 0.263)\tLoss 9.3915e-03 (6.8606e-03)\tAcc@1 100.00 ( 99.93)\n",
      "[400/714]\tTime  0.094 ( 0.255)\tLoss 2.2316e-03 (8.5766e-03)\tAcc@1 100.00 ( 99.90)\n",
      "[500/714]\tTime  0.093 ( 0.256)\tLoss 1.6543e-02 (9.6378e-03)\tAcc@1 100.00 ( 99.88)\n",
      "[600/714]\tTime  1.924 ( 0.256)\tLoss 2.2171e-04 (8.1967e-03)\tAcc@1 100.00 ( 99.90)\n",
      "[700/714]\tTime  0.093 ( 0.251)\tLoss 1.4192e-04 (7.0721e-03)\tAcc@1 100.00 ( 99.92)\n",
      " * Acc@1 99.849 Acc@5 100.000\n",
      "Epoch:  17\n",
      "[  0/714]\tTime  3.242 ( 3.242)\tLoss 1.7113e-04 (1.7113e-04)\tAcc@1 100.00 (100.00)\n",
      "[100/714]\tTime  0.093 ( 0.272)\tLoss 3.8360e-04 (5.8668e-03)\tAcc@1 100.00 ( 99.96)\n",
      "[200/714]\tTime  1.278 ( 0.263)\tLoss 2.1412e-04 (4.7459e-03)\tAcc@1 100.00 ( 99.97)\n",
      "[300/714]\tTime  0.096 ( 0.259)\tLoss 4.6885e-04 (4.0164e-03)\tAcc@1 100.00 ( 99.97)\n",
      "[400/714]\tTime  0.096 ( 0.254)\tLoss 3.1678e-04 (4.4463e-03)\tAcc@1 100.00 ( 99.97)\n",
      "[500/714]\tTime  0.092 ( 0.255)\tLoss 3.9301e-04 (5.5568e-03)\tAcc@1 100.00 ( 99.95)\n",
      "[600/714]\tTime  0.093 ( 0.254)\tLoss 9.0618e-04 (6.6478e-03)\tAcc@1 100.00 ( 99.93)\n",
      "[700/714]\tTime  0.093 ( 0.252)\tLoss 2.9970e-03 (7.8764e-03)\tAcc@1 100.00 ( 99.91)\n",
      " * Acc@1 99.975 Acc@5 100.000\n",
      "Epoch:  18\n",
      "[  0/714]\tTime  3.208 ( 3.208)\tLoss 1.0766e-03 (1.0766e-03)\tAcc@1 100.00 (100.00)\n",
      "[100/714]\tTime  0.170 ( 0.274)\tLoss 1.0760e-02 (1.3035e-02)\tAcc@1 100.00 ( 99.88)\n",
      "[200/714]\tTime  0.094 ( 0.256)\tLoss 6.8038e-04 (1.1619e-02)\tAcc@1 100.00 ( 99.89)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception in thread Thread-40:\n",
      "Traceback (most recent call last):\n",
      "  File \"/usr/lib/python3.6/threading.py\", line 916, in _bootstrap_inner\n",
      "    self.run()\n",
      "  File \"/usr/lib/python3.6/threading.py\", line 864, in run\n",
      "    self._target(*self._args, **self._kwargs)\n",
      "  File \"/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py\", line 158, in _pin_memory_loop\n",
      "    r = in_queue.get(timeout=MP_STATUS_CHECK_INTERVAL)\n",
      "  File \"/usr/lib/python3.6/multiprocessing/queues.py\", line 113, in get\n",
      "    return _ForkingPickler.loads(res)\n",
      "  File \"/usr/local/lib/python3.6/dist-packages/torch/multiprocessing/reductions.py\", line 256, in rebuild_storage_fd\n",
      "    fd = df.detach()\n",
      "  File \"/usr/lib/python3.6/multiprocessing/resource_sharer.py\", line 57, in detach\n",
      "    with _resource_sharer.get_connection(self._id) as conn:\n",
      "  File \"/usr/lib/python3.6/multiprocessing/resource_sharer.py\", line 87, in get_connection\n",
      "    c = Client(address, authkey=process.current_process().authkey)\n",
      "  File \"/usr/lib/python3.6/multiprocessing/connection.py\", line 487, in Client\n",
      "    c = SocketClient(address)\n",
      "  File \"/usr/lib/python3.6/multiprocessing/connection.py\", line 614, in SocketClient\n",
      "    s.connect(address)\n",
      "FileNotFoundError: [Errno 2] No such file or directory\n",
      "\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-8-5ca17cd78347>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     58\u001b[0m         \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Epoch: '\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mepoch\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     59\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 60\u001b[0;31m         \u001b[0mtrain\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtrain_loader\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcriterion\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moptimizer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mepoch\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     61\u001b[0m         \u001b[0mval_acc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mvalidate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mval_loader\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcriterion\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     62\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m<ipython-input-7-bd766b0a0eec>\u001b[0m in \u001b[0;36mtrain\u001b[0;34m(train_loader, model, criterion, optimizer, epoch)\u001b[0m\n\u001b[1;32m     85\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     86\u001b[0m     \u001b[0mend\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 87\u001b[0;31m     \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtarget\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32min\u001b[0m \u001b[0menumerate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtrain_loader\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     88\u001b[0m         \u001b[0minput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcuda\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnon_blocking\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     89\u001b[0m         \u001b[0mtarget\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtarget\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcuda\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnon_blocking\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py\u001b[0m in \u001b[0;36m__next__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    629\u001b[0m         \u001b[0;32mwhile\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    630\u001b[0m             \u001b[0;32massert\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshutdown\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbatches_outstanding\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 631\u001b[0;31m             \u001b[0midx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbatch\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get_batch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    632\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbatches_outstanding\u001b[0m \u001b[0;34m-=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    633\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0midx\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrcvd_idx\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py\u001b[0m in \u001b[0;36m_get_batch\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    599\u001b[0m             \u001b[0;32mwhile\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpin_memory_thread\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mis_alive\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    600\u001b[0m                 \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 601\u001b[0;31m                     \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata_queue\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mMP_STATUS_CHECK_INTERVAL\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    602\u001b[0m                 \u001b[0;32mexcept\u001b[0m \u001b[0mqueue\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mEmpty\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    603\u001b[0m                     \u001b[0;32mcontinue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/usr/lib/python3.6/queue.py\u001b[0m in \u001b[0;36mget\u001b[0;34m(self, block, timeout)\u001b[0m\n\u001b[1;32m    171\u001b[0m                     \u001b[0;32mif\u001b[0m \u001b[0mremaining\u001b[0m \u001b[0;34m<=\u001b[0m \u001b[0;36m0.0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    172\u001b[0m                         \u001b[0;32mraise\u001b[0m \u001b[0mEmpty\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 173\u001b[0;31m                     \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnot_empty\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwait\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mremaining\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    174\u001b[0m             \u001b[0mitem\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    175\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnot_full\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnotify\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/usr/lib/python3.6/threading.py\u001b[0m in \u001b[0;36mwait\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m    297\u001b[0m             \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    298\u001b[0m                 \u001b[0;32mif\u001b[0m \u001b[0mtimeout\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 299\u001b[0;31m                     \u001b[0mgotit\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mwaiter\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0macquire\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    300\u001b[0m                 \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    301\u001b[0m                     \u001b[0mgotit\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mwaiter\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0macquire\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "skf = StratifiedKFold(n_splits=10, random_state=None, shuffle=False)\n",
    "for flod_idx, (train_idx, val_idx) in enumerate(skf.split(train_path, train_label)):\n",
    "    print(flod_idx, train_idx, val_idx)\n",
    "    \n",
    "    train_loader = torch.utils.data.DataLoader(\n",
    "        QRDataset(train_path[train_idx], train_label[train_idx],\n",
    "                transforms.Compose([\n",
    "                            transforms.RandomGrayscale(),\n",
    "                            # transforms.Resize((124, 124)),\n",
    "                            # transforms.RandomAffine(10),\n",
    "                            transforms.ColorJitter(hue=.05, saturation=.05),\n",
    "                            transforms.Resize(280),\n",
    "                            transforms.RandomCrop((256, 256)),\n",
    "                            transforms.RandomHorizontalFlip(),\n",
    "                            transforms.RandomVerticalFlip(),\n",
    "                            transforms.ToTensor(),\n",
    "                            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])\n",
    "            ])\n",
    "        ), batch_size=50, shuffle=True, num_workers=10, pin_memory=True\n",
    "    )\n",
    "    \n",
    "    val_loader = torch.utils.data.DataLoader(\n",
    "        QRDataset(train_path[val_idx], train_label[val_idx],\n",
    "                transforms.Compose([\n",
    "                            # transforms.Resize((124, 124)),\n",
    "                            transforms.Resize(280),\n",
    "                            transforms.RandomCrop((256, 256)),\n",
    "                            transforms.ToTensor(),\n",
    "                            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])\n",
    "            ])\n",
    "        ), batch_size=10, shuffle=False, num_workers=10, pin_memory=True\n",
    "    )\n",
    "    \n",
    "    test_loader = torch.utils.data.DataLoader(\n",
    "        QRDataset(test_path, np.zeros(len(test_path)),\n",
    "                transforms.Compose([\n",
    "                            # transforms.Resize((124, 124)),\n",
    "                            transforms.Resize(280),\n",
    "                            transforms.RandomCrop((256, 256)),\n",
    "                            transforms.RandomHorizontalFlip(),\n",
    "                            transforms.RandomVerticalFlip(),\n",
    "                            transforms.ToTensor(),\n",
    "                            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])\n",
    "            ])\n",
    "        ), batch_size=10, shuffle=False, num_workers=10, pin_memory=True\n",
    "    )\n",
    "    \n",
    "    \n",
    "    model = VisitNet()\n",
    "    # model = nn.DataParallel(model).cuda()\n",
    "    model = model.cuda()\n",
    "    criterion = nn.CrossEntropyLoss().cuda()\n",
    "    optimizer = torch.optim.Adam(model.parameters(), 0.01)\n",
    "    \n",
    "    scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=2, gamma=0.85)\n",
    "    for epoch in range(30):\n",
    "        scheduler.step()\n",
    "        print('Epoch: ', epoch)\n",
    "\n",
    "        train(train_loader, model, criterion, optimizer, epoch)\n",
    "        val_acc = validate(val_loader, model, criterion)\n",
    "        \n",
    "        torch.save(model.state_dict(), './model_dump/resnet18_fold{0}_{1}_{2}.pt'.format(flod_idx, str(epoch).zfill(2), val_acc).replace(' ', ''))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-28T10:51:20.594905Z",
     "start_time": "2019-05-28T10:44:06.909834Z"
    }
   },
   "outputs": [],
   "source": [
    "model.load_state_dict(torch.load('./model_dump/resnet18_fold0_11_Acc@1100.00(100.00).pt'))\n",
    "\n",
    "test_pred = predict(test_loader, model, 10)\n",
    "test_pred = np.vstack(test_pred)\n",
    "test_pred = np.argmax(test_pred, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-28T10:51:24.057465Z",
     "start_time": "2019-05-28T10:51:23.978592Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "test_pred = lbl.inverse_transform(test_pred)\n",
    "test_csv = pd.DataFrame()\n",
    "test_csv['name'] = [x.split('/')[-1] for x in test_path]\n",
    "test_csv['label'] = test_pred\n",
    "test_csv.sort_values(by='name', inplace=True)\n",
    "test_csv.to_csv('tmp_newmodel_resnet18_tta10.csv', index=None, sep=',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-05-19T15:31:56.918088Z",
     "start_time": "2019-05-19T15:31:56.697434Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name, label\r",
      "\r\n",
      "013MNV9B.jpg, 100\r",
      "\r\n",
      "016ETNGG.jpg, 50\r",
      "\r\n",
      "018SUTBA.jpg, 0.1\r",
      "\r\n",
      "0192G5IC.jpg, 5\r",
      "\r\n",
      "01953EH7.jpg, 100\r",
      "\r\n",
      "01AUV9WG.jpg, 10\r",
      "\r\n",
      "01B68AKT.jpg, 1\r",
      "\r\n",
      "01DMQGVG.jpg, 0.1\r",
      "\r\n",
      "01E9AUX7.jpg, 0.1\r",
      "\r\n"
     ]
    }
   ],
   "source": [
    "!head ../input/train_face_value_label.csv"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
